<template>
    <div>
        <Button v-if="showBackBtn" class="goBack" type="primary" @click="$router.back()"><Icon type="ios-arrow-back" /> 返回  </Button>
        <Card class="line-style">
            <div class="line-wrap">
                <div class="line-title">口罩处置</div>
                <div class="line-main">
                    <Form ref="formCustom" :model="formCustom"   :rules="ruleCustom" :label-width="100" >
                        <Row>
                            <Col span="7">
                                <Form-item label="上报日期" prop="imp_date">
                                    <DatePicker @on-change="dateChange" :value="formCustom.imp_date" format="yyyy-MM-dd"
                                                type="date" :options="optionsDate"></DatePicker>
                                </Form-item>
                            </Col>
                            <Col span="7">
                                <Form-item label="收集量"  prop="quantity">
                                    <i-input v-model="formCustom.quantity" type="number" number><span slot="append">公斤</span></i-input>
                                </Form-item>
                            </Col>
                            <Col span="7">
                                <Form-item label="焚烧量"   prop="burn_quantity">
                                    <i-input v-model="formCustom.burn_quantity" type="number" number><span slot="append">公斤</span></i-input>
                                </Form-item>
                            </Col>
                        </Row>
                    </Form>
                    <div class="line-button" v-if="!showDetail">
                        <Button type="primary" @click="validateForm('formCustom')">保存</Button>
                    </div>
                </div>
            </div>
        </Card>
        <Modal
                v-model="modal"
                title="确认操作"
                width="20"
                @on-ok="handleSubmit">
            <div style="text-align:center">
                <p>是否确认保存!</p>
            </div>
        </Modal>
    </div>
</template>

<script>
import { insert, update, getById } from '@/api/outbreak-management/city-operation/environmental-improvement/disposal-of-masks'
// 口罩处置添加
export default {
  data () {
    const validateFloat = (rule, value, callback) => {
      const reg = /^(0|\+?[1-9][0-9]{0,8})(\.\d{1,2})?$/
      if (!reg.test(value)) {
        callback(new Error('最多只允许两位小数'))
      } else {
        callback()
      }
    }
    return {
      optionsDate: {
        disabledDate (date) {
          return date && date.valueOf() > Date.now()
        }
      },
      // 弹窗控制
      modal: false,
      // 控制保存按钮显隐
      showDetail: false,
      // 控制返回按钮显隐
      showBackBtn: false,
      // 添加表单/修改表单
      formCustom: {
        id: '',
        quantity: 0,
        burn_quantity: 0,
        imp_date: ''
      },
      ruleCustom: {
        quantity: [
          { required: true, type: 'number', message: '收集量必填', trigger: 'blur' },
          { min: 0, max: 99999999, type: 'number', message: '收集量范围0-99999999', trigger: 'blur' },
          { validator: validateFloat, trigger: 'blur' }
        ],
        burn_quantity: [
          { required: true, type: 'number', message: '焚烧量必填', trigger: 'blur' },
          { min: 0, max: 99999999, type: 'number', message: '焚烧量范围0-99999999', trigger: 'blur' },
          { validator: validateFloat, trigger: 'blur' }
        ],
        imp_date: [
          { required: true, message: '上报日期必选', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    validateForm (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          this.modal = true
        } else {
          this.$Message.error('表单验证失败,请检查后再试!')
        }
      })
    },
    /**
             * 日期格式化
             */
    dateChange (date) {
      this.formCustom.imp_date = date
    },
    handleSubmit () {
      this.formCustom.id = this.$route.query.id
      let requestData = this.formCustom
      if (this.$route.query.id) {
        update(requestData).then(({ data }) => {
          let { errcode } = data
          if (errcode === 0) {
            this.$Message.success('保存成功!')
          } else {
            this.$Message.error('保存失败!')
          }
        })
      } else {
        insert(requestData).then(({ data }) => {
          let { errcode } = data
          if (errcode === 0) {
            this.$Message.success('保存成功!')
          } else {
            this.$Message.error('保存失败!')
          }
        })
      }
    },
    isEditStatus () {
      if (this.$route.query && this.$route.query.id) {
        console.log(this.$route.query.id)
        if (this.$route.query.showDetail) {
          this.showDetail = true
        }
        this.showBackBtn = true
        this.editShow()
      }
    },
    editShow () {
      getById({ id: this.$route.query.id }).then(({ data }) => {
        let { data: res } = data
        this.formCustom = res
        this.formCustom.imp_date = this.$common.moment(res.imp_date).format('YYYY-MM-DD')
      })
    }
  },
  created () {
    this.isEditStatus()
  },
  mounted () {
  }
}
</script>

<style scoped>

</style>
